package com.dimeng;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Gallery;
import android.widget.Toast;

import com.dimeng.adapter.GalleryAdapter;
import com.dimeng.entity.MettlePhotoAlumb;
import com.dimeng.utils.Constant;
import com.dimeng.utils.LoadDataThread;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @author write by ccn<br>
 *         <b>helloandroid100@gmail.com</b>
 * @version 0.1;2012-7-4 上午11:51:06<br>
 *          descrition:浏览相册的图片
 */
public class GalleryMettlePhotoActivity extends SuperActivity {
	private static final long serialVersionUID = 6589602664647178941L;
	private Gallery gallery;
	private GalleryAdapter adapter;
	private ArrayList<String> urlList = new ArrayList<String>();
	private Intent intent;
	private String ablumId;
	private Thread loadDataThread;
	private String url = "";
	private Gson gson = new Gson();
	
	private static Map<String,List<String>> dataMap = new HashMap<String,List<String>>();
	
	private Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			switch (msg.what) {
				case 0:
	            	Toast.makeText(GalleryMettlePhotoActivity.this, "已加载所有数据", Toast.LENGTH_SHORT).show();
					break;
	            case 1:
					String gsonStr = (String) msg.obj;
	        		List<MettlePhotoAlumb> loadList = gson.fromJson(gsonStr, new TypeToken<List<MettlePhotoAlumb>>(){}.getType());
	        		if(null != loadList){
	        			for(MettlePhotoAlumb mpa : loadList){
	        				urlList.add(mpa.getImageUrl());
	        			}
	        			dataMap.put(ablumId, urlList);
	        			adapter.notifyDataSetChanged();
	        			System.out.println("new download urlList.size="+urlList.size());
	        		}
	                break;
	            case 2:
	            	Toast.makeText(GalleryMettlePhotoActivity.this, Constant.NET_ERROR, Toast.LENGTH_LONG).show();
	            	break;
	            default:
	            	break;
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery_mettle_photo);
		gallery = (Gallery) findViewById(R.id.galleryId);
		
		intent = getIntent();
		ablumId = intent.getStringExtra("ablumId");
		url = Constant.HOST_URL+"mettlePhotos.do?ablumId="+ablumId;
		
		List<String> cacheUrlList = dataMap.get(ablumId);
		if(null != cacheUrlList){
			urlList.clear();
			urlList.addAll(cacheUrlList);
			System.out.println("from cache urlList.size="+urlList.size());
		} else {
			startLoadDataThread();
		}
		adapter = new GalleryAdapter(this,urlList,gallery);
		gallery.setAdapter(adapter);
		adapter.notifyDataSetChanged();
		
		gallery.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> adpter, View view, int position, long id) {
				System.out.println("position="+position + " image url="+urlList.get(position));
				;
			}
			public void onNothingSelected(AdapterView<?> arg0) {}
		});
		
	}
	
	/**开启下载数据的线程**/
	public void startLoadDataThread(){
		if(null == loadDataThread || !loadDataThread.isAlive()){
			loadDataThread = new LoadDataThread(url,Constant.CHARSET_GBK,handler,true);
			loadDataThread.start();
		}
	}
}
